<template>
  <div class="my-goods-list">
    <MyTable :arr="arr">
      <template #thead>
        <th>#</th>
        <th>商品名称</th>
        <th>价格</th>
        <th>标签</th>
        <th>操作</th>
      </template>
      <template #tbody="scope">
        <td>{{ scope.obj.id }}</td>
        <td>{{ scope.obj.goods_name }}</td>
        <td>{{ scope.obj.goods_price }}</td>
        <td>
          <div>
            <!-- 输入框 -->
            <input
              style="width: 150px"
              class="tag-input form-control"
              type="text"
              v-model.trim="scope.obj.inputValue"
              v-if="scope.obj.inputVisible"
              v-focus
              @blur="scope.obj.inputVisible = false"
              @keyup.enter="addFn(scope.obj)"
            />
            <button
              v-else
              @click="scope.obj.inputVisible = true"
              class="btn btn-primary btn-sm add-tag"
            >
              +Tag
            </button>
          </div>

          <span
            class="badge bg-warning"
            v-for="(item, index) in scope.obj.tags"
            :key="index"
            >{{ item }}</span
          >
        </td>
        <td>
          <button @click="delFn(scope.obj.id)" class="btn btn-danger btn-sm">
            删除
          </button>
        </td>
      </template>
    </MyTable>
  </div>
</template>

<script>
// 接口：GTE  https://www.escook.cn/api/goods
import MyTable from "@/components/MyTable.vue";
import axios from "axios";
export default {
  directives: {
    focus: {
      inserted(ele) {
        ele.focus();
      },
    },
  },
  methods: {
    delFn(id) {
      this.arr = this.arr.filter((item) => item.id !== id);
    },
    addFn(obj) {
      if (obj.inputValue === "") return;
      obj.tags.push(obj.inputValue);
      obj.inputValue = "";
    },
  },
  components: {
    MyTable,
  },
  data() {
    return {
      arr: [],
    };
  },
  async created() {
    const { data: res } = await axios.get("https://www.escook.cn/api/goods");
    this.arr = res.data;
  },
};
</script>

<style>
</style>
